package com.martini.satoken01.service;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 默认缓存服务
 *
 * @author martini at 2021/12/17 23:11
 */
public class DefaultLimitInfoCacheServiceImpl implements LimitInfoCacheService {

    private final ConcurrentHashMap<Object, Object> cache = new ConcurrentHashMap<>(16);

    @Override
    public String info() {
        return "defaultLimitInfoCacheService";
    }

    @Override
    public void put(Object key, Object value) {
        this.cache.put(key, value);
    }

    @Override
    public Object get(Object key) {
        return this.get(key, Object.class);
    }

    @Override
    @SuppressWarnings("all")
    public <T> T get(Object key, Class<T> classType) {
        Object obj = this.cache.get(key);
        if (obj != null && (classType == null || !classType.isInstance(obj))) {
            throw new NoSuchBeanDefinitionException("类型未定义异常", "类型未定义异常");
        }
        return obj == null ? null : (T) obj;
    }

}
